package com.xxx.modules.sys.service.impl;

import com.xxx.modules.sys.domain.SysTextDomain;
import com.xxx.modules.sys.service.SysTextService;
import com.xxx.modules.sys.service.SysUserTextService;
import com.xxx.modules.sys.service.UserTextService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * Created by ChenXuefei on 2017/10/16.
 */
@Service("userTextService")
public class UserTextServiceImpl implements UserTextService {

    @Autowired
    private SysTextService sysTextService;
    @Autowired
    private SysUserTextService sysUserTextService;

    @Override
    public void addText(SysTextDomain sysTextDomain) {
        sysTextDomain.setState(0);//  0 正常  1 删除
        sysTextDomain.setCreateTime(new Date());
        sysTextDomain.setUpdateTime(sysTextDomain.getCreateTime());
        sysTextService.add(sysTextDomain);

    }

    @Override
    public void updateText(SysTextDomain sysTextDomain) {
        sysTextDomain.setUpdateTime(new Date());
        sysTextService.updateBySelective(sysTextDomain);
    }

    @Override
    public void deleteText(String textId, String userId) {
        SysTextDomain sysTextDomain = new SysTextDomain();
        sysTextDomain.setId(Long.valueOf(textId));
        sysTextDomain.setUserId(Long.valueOf(userId));
        sysTextDomain.setState(1);//状态 1 删除
        sysTextDomain.setUpdateTime(new Date());
        sysTextService.updateBySelective(sysTextDomain);
    }

    @Override
    public List<SysTextDomain> queryText(String userId) {
        SysTextDomain sysTextDomain = new SysTextDomain();
        sysTextDomain.setUserId(Long.valueOf(userId));
        List<SysTextDomain> sysTextDomains = sysTextService.queryByList(sysTextDomain);
        // java 8  新特性
        sysTextDomains.sort((SysTextDomain text1,SysTextDomain text2)
                -> text2.getUpdateTime().compareTo(text1.getUpdateTime()));
        return sysTextDomains;
    }

    @Override
    public SysTextDomain queryTextInfoByTextId(String id) {
        return sysTextService.queryById(id);
    }
}
